#!/bin/bash

DELETE_OLD="n"
RM_DB="n"
NEWROOTFS="./my_rootfs"
#TINDERBOX="./TinderGenMoko"
TINDERBOX="./myconfig"
ADDPKGS="dropbear \
          mtd-utils \
          sqlite \
          openrc \
          lrzsz \
          glib
          "
#"dhcpcd iptables  usbutils  vim  wireless-tools wpa_supplicant libusb 

EMRGCMD="emerge-armv5tl-softfloat-linux-gnueabi"
LOG="log.`echo ${0} | sed -e 's/\.\///'`"
DEFAULT_SERVICES="dropbear hald"
BOOT_SERVICES="net.eth0 net.usb0"
SYS_SERVICES="udev sysfs"
GCC_LIBS_DIR="/usr/lib/gcc/armv5tl-softfloat-linux-gnueabi/4.4.2"
GLIBC_LIBS_DIR="/usr/armv5tl-softfloat-linux-gnueabi/lib"


echo "starting `date` " > ${LOG}

if [ $DELETE_OLD = "y" ]  ; then
    echo "Pruning ${NEWROOTFS}" 2>&1 | tee -a ${LOG}
    sudo rm -rfv ${NEWROOTFS}   2>&1 | tee -a ${LOG} 
fi
 
echo "Setting up basic directories" 2>&1 | tee -a ${LOG}
for i in boot etc dev proc root sys media/card media/card2; do
    if [ ! -d ${NEWROOTFS}/${i} ]  ; then
        sudo mkdir -vp ${NEWROOTFS}/${i} 2>&1 | tee -a ${LOG};
    fi
done;

if [ -d $GLIBC_LIBS_DIR ] ; then
    if [ ! -f ${NEWROOTFS}/lib/ld-2.11.so ] ; then
        sudo cp -rvf $GLIBC_LIBS_DIR/*  ${NEWROOTFS}/lib/ 2>&1 | tee -a ${LOG}
    fi
fi

if [ ! -c ${NEWROOTFS}/dev/null ] ; then
    sudo mknod ${NEWROOTFS}/dev/null c 1 3  2>&1 | tee -a ${LOG}; 
fi

if [ ! -c ${NEWROOTFS}/dev/console ] ; then
    sudo mknod ${NEWROOTFS}/dev/console c 5 1   2>&1 | tee -a ${LOG}; 
fi
 
echo "Setting up basic configuration" 2>&1 | tee -a ${LOG}
if [ -d ${TINDERBOX} ] ; then
    for i in $( ls ${TINDERBOX}/etc/ | grep -iv portage ); do
      sudo cp -vR ${TINDERBOX}/etc/${i} ${NEWROOTFS}/etc/ 2>&1 | tee -a ${LOG}; 
    done;
fi
 
echo "Setting up portage configuration" 2>&1 | tee -a ${LOG}
sudo cp -LRv ${TINDERBOX}/etc/portage ${NEWROOTFS}/etc/ 2>&1 | tee -a ${LOG}
sudo sed -i -e 's/\(EMERGE_DEFAULT_OPTS=.*\)"$/\1 --getbinpkg"/' \
       -e 's/buildpkg//' \
       -e 's/INSTALL_MASK="/INSTALL_MASK="*.h /' \
         ${NEWROOTFS}/etc/make.conf  2>&1 | tee -a ${LOG}

if [ ! -f ${NEWROOTFS}/etc/resolv.conf ] ; then
    sudo cp -Lv /etc/resolv.conf ${NEWROOTFS}/etc/ 2>&1 | tee -a ${LOG}
fi
 
echo "Updating remote binpkgs" 2>&1 | tee -a ${LOG}
#./sync-binpkgs | tee -a ${LOG}
echo "Installing bare @system" 2>&1 | tee -a ${LOG}
#sudo bash -c "ROOT=${NEWROOTFS} USE="-X" ${EMRGCMD} -vg vim system " | tee  -a ${LOG}
echo "adding my packages" 2>&1 | tee -a ${LOG}
sudo bash -c "ROOT=${NEWROOTFS} USE="-X" ${EMRGCMD} -atvg --noreplace ${ADDPKGS}" 2>&1 | tee -a ${LOG}

echo "Fixing GCC" 2>&1 | tee -a ${LOG}
#for i in $( find /usr/lib* -name libgcc_s*.so* | grep armv4tl ); do
#  if [ -d `dirname ${i}` ] ; then
#    mkdir -p `dirname ${i}`
#  fi
#  echo "  "`echo ${i} | sed -e 's/.*\///'`;
#  cp -v ${i} ${NEWROOTFS}/`echo ${i} | sed -e 's/lib64/lib/'`;
#done;
#for i in $( find /usr/lib* -name libstdc++*.so* | grep armv4tl ); do
#  if [ -d `dirname ${i}` ] ; then
#    mkdir -p `dirname ${i}`
#  fi
#  echo "  "`echo ${i} | sed -e 's/.*\///'`;
#  cp -v ${i} ${NEWROOTFS}/`echo ${i} | sed -e 's/lib64/lib/'`;
#done;
#   cp -v ${GCC_LIBS_DIR}/libgcc_s.so.1 \
#     ${NEWROOTFS}/${GCC_LIBS_DIR}/libgcc_s.so.1
#
#   pushd ${NEWROOTFS}/${GCC_LIBS_DIR}
#   ln -s ./libgcc_s.so.1 \
#     ${NEWROOTFS}/${GCC_LIBS_DIR}/libgcc_s.so
#   popd
#
#   cp -v ${GCC_LIBS_DIR}/libstdc++.so.6.0.13 \
#     ${NEWROOTFS}/${GCC_LIBS_DIR}/libstdc++.so.6.0.13
#   ln -s ./libstdc++.so.6.0.13 \
#     ${NEWROOTFS}/${GCC_LIBS_DIR}/libstdc++.so.6
#   ln -s ./libstdc++.so.6.0.13 \
#     ${NEWROOTFS}/${GCC_LIBS_DIR}/libstdc++.so


if [ ! -f ${NEWROOTFS}/lib/libgcc_s.so* ] ; then
  sudo cp -rfv ${GCC_LIBS_DIR}/libgcc_s.so*  ${NEWROOTFS}/lib/
fi

if [ ! -f ${NEWROOTFS}/lib/libstdc++.so* ] ; then
    sudo cp -rfv ${GCC_LIBS_DIR}/libstdc++.so* ${NEWROOTFS}/lib/
fi



echo "Installing base applications" 2>&1 | tee -a ${LOG}
# hack(s) to install sturdy app(s)
#sudo bash -c "EXTRA_ECONF="--enable-unique" ROOT=${NEWROOTFS} ${EMRGCMD} -v eix " | tee -a ${LOG}
 
#   echo "Installing custom files"
#   cp -Rv dingbat_config/* ${NEWROOTFS}/ | tee -a ${LOG}
#   cp -Lv /etc/wpa_supplicant/wpa_supplicant.conf ${NEWROOTFS}/etc/wpa_supplicant/ | tee -a ${LOG}
#    
#   echo "Installing services"
#   ln -s ./net.lo ${NEWROOTFS}/etc/init.d/net.eth0
#   ln -s ./net.lo ${NEWROOTFS}/etc/init.d/net.usb0
#   for i in ${DEFAULT_SERVICES}; do
#     ln -s /etc/init.d/${i} ${NEWROOTFS}/etc/runlevels/default/;
#   done;
#   for i in ${BOOT_SERVICES}; do
#     ln -s /etc/init.d/${i} ${NEWROOTFS}/etc/runlevels/boot/;
#   done;
#   for i in ${SYS_SERVICES}; do
#     ln -s /etc/init.d/${i} ${NEWROOTFS}/etc/runlevels/sysinit/;
#   done;
 

if [  -d ${NEWROOTFS}/usr/share ] ; then
    sudo rm -rf ${NEWROOTFS}/usr/share/
fi
if [  -d ${NEWROOTFS}/usr/include ] ; then
    sudo rm -rf ${NEWROOTFS}/usr/include
fi


if [ -d ${NEWROOTFS}/usr/lib ] ; then
    pushd ${NEWROOTFS}/usr/lib
    find . -name "*.a" | xargs sudo rm -f
    popd
fi

if [ ${RM_DB} = "y" ] ; then
    if [ -d ${NEWROOTFS}/var/db/pkg ] ; then
        sudo rm -rf ${NEWROOTFS}/var/db/pkg 
    fi
fi

echo "Finding newest kernel" 2>&1 | tee -a ${LOG}
# FIXME - for now we just dump what we find
echo "Loading modules" 2>&1 | tee -a ${LOG}
#tar -xzpvf AndyTracking/GTA02/modules-GTA02_andy-tracking-*.tar.gz -C ${NEWROOTFS}/ | tee -a ${LOG}

 
echo "uncompressed size: `sudo du -sh ${NEWROOTFS}/`"  2>&1 | tee -a ${LOG}
 
echo "Creating image" 2>&1 | tee -a ${LOG}
#mkfs.jffs2 -r ${NEWROOTFS}/ --pad=0x700000 -o BareGenMoko.jffs2 -e 0x20000 -n
echo "compressed size: "du -b BareGenMoko.jffs2" Bytes" 2>&1 | tee -a ${LOG}
echo "Done."  2>&1 | tee -a ${LOG}

#sudo cp -vf $0 /usr/armv5tl-softfloat-linux-gnueabi/etc/portage 
#sudo cp -vf setup.rootfs_zlb /usr/armv5tl-softfloat-linux-gnueabi/etc/portage 
#find /var/db/pkg -name '*.ebuild' -exec grep -qF 'pkg_postinst()' {} \; -exec ebuild {} postinst \;

